package com.kuqi.crs.settings.service.impl;

import com.kuqi.crs.settings.dao.DicTypeDao;
import com.kuqi.crs.settings.dao.DicValueDao;
import com.kuqi.crs.settings.domain.DicType;
import com.kuqi.crs.settings.domain.DicValue;
import com.kuqi.crs.settings.service.DicService;
import com.kuqi.crs.util.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:kuqi
 * @createTime:2021/10/6 16:07
 * @description:字典的业务逻辑层，将数据从数据库中取出，打包成map<key为单个类型名称String，value为该名称对应的value集合List>
 */
public class DicServiceImpl implements DicService{
    /**
     *Dao的动态代理
     */
    private DicTypeDao dicTypeDao = SqlSessionUtil.getSqlSession().getMapper(DicTypeDao.class);
    private DicValueDao dicValueDao = SqlSessionUtil.getSqlSession().getMapper(DicValueDao.class);

    public Map<String, List<DicValue>> getAll() {
        Map<String, List<DicValue>> map = new HashMap<String, List<DicValue>>();
        //获取所有的字典类型，DicType
        List<DicType> dicTypeList = dicTypeDao.getDicTypeList();
        //遍历字典的类型DicType，获取每个类型对应的所有的值
        for (DicType dicType : dicTypeList) {
            //每次遍历获取每种类型的编码
            String code = dicType.getCode();
            //根据编码查找其所对应的value的值，存放在集合中
            List<DicValue> dicValueList = dicValueDao.getDicValueList(code);
            //将字典的类型以及所对应的值得集合存放到map中
            /**
             * 理解：
             *      map.put("name",["张三"，"李四"，"网二"])
             *      map.put("sex",["男","女"])
             */
            map.put(code,dicValueList);
        }
        return map;
    }
}
